package cn.achuan.spring.springcloud.demo;

import cn.achuan.spring.springcloud.demo.product.Product;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/products/2")
public class ProductTwoController {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod="findByItemCodeFallback")
    @RequestMapping(value = "/{itemCode}", method = RequestMethod.GET)
    public Product findByItemCode(@PathVariable String itemCode) {
        return this.restTemplate.getForObject("http://PRODUCT-SERVICE/products/" + itemCode, Product.class);
    }

    public Product findByItemCodeFallback(String itemCode) {
        return new Product("error", "未知", "BandOne-Fallback2", 0);
    }

}
